package com.android.youwenchuang.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.android.youwenchuang.bean.User;
import com.android.youwenchuang.databinding.FragmentMessageBinding;
import com.android.youwenchuang.presenter.MessagePresenter;
import com.android.youwenchuang.ui.activity.MessageActivity;
import com.android.youwenchuang.ui.adapter.MessageAdapter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

public class MessageFragment extends Fragment {

    private FragmentMessageBinding viewBinding;

    private MessageAdapter adapter = new MessageAdapter();

    private MessagePresenter presenter = new MessagePresenter();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        viewBinding = FragmentMessageBinding.inflate(LayoutInflater.from(requireContext()));
        return viewBinding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        viewBinding.rvList.setLayoutManager(new LinearLayoutManager(requireContext()));
        viewBinding.rvList.setAdapter(adapter);
        //用户列表
        adapter.setNewInstance(presenter.getUserList());
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                Intent intent = new Intent(requireContext(), MessageActivity.class);
                User user = adapter.getItem(position);
                intent.putExtra("data", user.name);
                startActivity(intent);
            }
        });
    }
}
